package edu.unf.cen6940.service;

import edu.unf.cen6940.dao.FacultyDAO;
import edu.unf.cen6940.dao.ThesisDAO;
import edu.unf.cen6940.dao.ThesiscommitteeDAO;

import edu.unf.cen6940.domain.Faculty;
import edu.unf.cen6940.domain.Thesis;
import edu.unf.cen6940.domain.Thesiscommittee;

import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

import org.springframework.transaction.annotation.Transactional;

/**
 * Spring service that handles CRUD requests for Thesiscommittee entities
 * 
 */

@Service("ThesiscommitteeService")
@Transactional
public class ThesiscommitteeServiceImpl implements ThesiscommitteeService {

	/**
	 * DAO injected by Spring that manages Faculty entities
	 * 
	 */
	@Autowired
	private FacultyDAO facultyDAO;

	/**
	 * DAO injected by Spring that manages Thesis entities
	 * 
	 */
	@Autowired
	private ThesisDAO thesisDAO;

	/**
	 * DAO injected by Spring that manages Thesiscommittee entities
	 * 
	 */
	@Autowired
	private ThesiscommitteeDAO thesiscommitteeDAO;

	/**
	 * Instantiates a new ThesiscommitteeServiceImpl.
	 *
	 */
	public ThesiscommitteeServiceImpl() {
	}

	/**
	 * Return all Thesiscommittee entity
	 * 
	 */
	@Transactional
	public List<Thesiscommittee> findAllThesiscommittees(Integer startResult, Integer maxRows) {
		return new java.util.ArrayList<Thesiscommittee>(thesiscommitteeDAO.findAllThesiscommittees(startResult, maxRows));
	}

	/**
	 * Save an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public void saveThesiscommittee(Thesiscommittee thesiscommittee) {
		Thesiscommittee existingThesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee.getThesisCommitteeId());

		if (existingThesiscommittee != null) {
			if (existingThesiscommittee != thesiscommittee) {
				existingThesiscommittee.setThesisCommitteeId(thesiscommittee.getThesisCommitteeId());
			}
			thesiscommittee = thesiscommitteeDAO.store(existingThesiscommittee);
		} else {
			thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		}
		thesiscommitteeDAO.flush();
	}

	/**
	 */
	@Transactional
	public Thesiscommittee findThesiscommitteeByPrimaryKey(Integer thesisCommitteeId) {
		return thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId);
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeFacultyByChair(Integer thesisCommitteeId, Faculty related_facultybychair) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Faculty existingfacultyByChair = facultyDAO.findFacultyByPrimaryKey(related_facultybychair.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyByChair != null) {
			existingfacultyByChair.setFacultyId(related_facultybychair.getFacultyId());
			existingfacultyByChair.setNnumber(related_facultybychair.getNnumber());
			existingfacultyByChair.setFirstName(related_facultybychair.getFirstName());
			existingfacultyByChair.setLastName(related_facultybychair.getLastName());
			existingfacultyByChair.setEmail(related_facultybychair.getEmail());
			existingfacultyByChair.setUrl(related_facultybychair.getUrl());
			existingfacultyByChair.setActive(related_facultybychair.getActive());
			related_facultybychair = existingfacultyByChair;
		}

		thesiscommittee.setFacultyByChair(related_facultybychair);
		related_facultybychair.getThesiscommitteesForChair().add(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybychair = facultyDAO.store(related_facultybychair);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Load an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Set<Thesiscommittee> loadThesiscommittees() {
		return thesiscommitteeDAO.findAllThesiscommittees();
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeFacultyByChair(Integer thesiscommittee_thesisCommitteeId, Integer related_facultybychair_facultyId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);
		Faculty related_facultybychair = facultyDAO.findFacultyByPrimaryKey(related_facultybychair_facultyId, -1, -1);

		thesiscommittee.setFacultyByChair(null);
		related_facultybychair.getThesiscommitteesForChair().remove(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybychair = facultyDAO.store(related_facultybychair);
		facultyDAO.flush();

		facultyDAO.remove(related_facultybychair);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeFacultyByCommitteeMember1(Integer thesisCommitteeId, Faculty related_facultybycommitteemember1) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Faculty existingfacultyByCommitteeMember1 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember1.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyByCommitteeMember1 != null) {
			existingfacultyByCommitteeMember1.setFacultyId(related_facultybycommitteemember1.getFacultyId());
			existingfacultyByCommitteeMember1.setNnumber(related_facultybycommitteemember1.getNnumber());
			existingfacultyByCommitteeMember1.setFirstName(related_facultybycommitteemember1.getFirstName());
			existingfacultyByCommitteeMember1.setLastName(related_facultybycommitteemember1.getLastName());
			existingfacultyByCommitteeMember1.setEmail(related_facultybycommitteemember1.getEmail());
			existingfacultyByCommitteeMember1.setUrl(related_facultybycommitteemember1.getUrl());
			existingfacultyByCommitteeMember1.setActive(related_facultybycommitteemember1.getActive());
			related_facultybycommitteemember1 = existingfacultyByCommitteeMember1;
		} else {
			related_facultybycommitteemember1 = facultyDAO.store(related_facultybycommitteemember1);
			facultyDAO.flush();
		}

		thesiscommittee.setFacultyByCommitteeMember1(related_facultybycommitteemember1);
		related_facultybycommitteemember1.getThesiscommitteesForCommitteeMember1().add(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember1 = facultyDAO.store(related_facultybycommitteemember1);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeFacultyByCommitteeMember3(Integer thesisCommitteeId, Faculty related_facultybycommitteemember3) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Faculty existingfacultyByCommitteeMember3 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember3.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyByCommitteeMember3 != null) {
			existingfacultyByCommitteeMember3.setFacultyId(related_facultybycommitteemember3.getFacultyId());
			existingfacultyByCommitteeMember3.setNnumber(related_facultybycommitteemember3.getNnumber());
			existingfacultyByCommitteeMember3.setFirstName(related_facultybycommitteemember3.getFirstName());
			existingfacultyByCommitteeMember3.setLastName(related_facultybycommitteemember3.getLastName());
			existingfacultyByCommitteeMember3.setEmail(related_facultybycommitteemember3.getEmail());
			existingfacultyByCommitteeMember3.setUrl(related_facultybycommitteemember3.getUrl());
			existingfacultyByCommitteeMember3.setActive(related_facultybycommitteemember3.getActive());
			related_facultybycommitteemember3 = existingfacultyByCommitteeMember3;
		} else {
			related_facultybycommitteemember3 = facultyDAO.store(related_facultybycommitteemember3);
			facultyDAO.flush();
		}

		thesiscommittee.setFacultyByCommitteeMember3(related_facultybycommitteemember3);
		related_facultybycommitteemember3.getThesiscommitteesForCommitteeMember3().add(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember3 = facultyDAO.store(related_facultybycommitteemember3);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeFacultyByCommitteeMember4(Integer thesiscommittee_thesisCommitteeId, Integer related_facultybycommitteemember4_facultyId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);
		Faculty related_facultybycommitteemember4 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember4_facultyId, -1, -1);

		thesiscommittee.setFacultyByCommitteeMember4(null);
		related_facultybycommitteemember4.getThesiscommitteesForCommitteeMember4().remove(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember4 = facultyDAO.store(related_facultybycommitteemember4);
		facultyDAO.flush();

		facultyDAO.remove(related_facultybycommitteemember4);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Delete an existing Thesiscommittee entity
	 * 
	 */
	@Transactional
	public void deleteThesiscommittee(Thesiscommittee thesiscommittee) {
		thesiscommitteeDAO.remove(thesiscommittee);
		thesiscommitteeDAO.flush();
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeFacultyByCommitteeMember1(Integer thesiscommittee_thesisCommitteeId, Integer related_facultybycommitteemember1_facultyId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);
		Faculty related_facultybycommitteemember1 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember1_facultyId, -1, -1);

		thesiscommittee.setFacultyByCommitteeMember1(null);
		related_facultybycommitteemember1.getThesiscommitteesForCommitteeMember1().remove(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember1 = facultyDAO.store(related_facultybycommitteemember1);
		facultyDAO.flush();

		facultyDAO.remove(related_facultybycommitteemember1);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Delete an existing Thesis entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeThesises(Integer thesiscommittee_thesisCommitteeId, Integer related_thesises_thesisId) {
		Thesis related_thesises = thesisDAO.findThesisByPrimaryKey(related_thesises_thesisId, -1, -1);

		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);

		related_thesises.getThesiscommittees().remove(thesiscommittee);
		thesiscommittee.getThesises().remove(related_thesises);

		thesisDAO.remove(related_thesises);
		thesisDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeFacultyByCommitteeMember3(Integer thesiscommittee_thesisCommitteeId, Integer related_facultybycommitteemember3_facultyId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);
		Faculty related_facultybycommitteemember3 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember3_facultyId, -1, -1);

		thesiscommittee.setFacultyByCommitteeMember3(null);
		related_facultybycommitteemember3.getThesiscommitteesForCommitteeMember3().remove(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember3 = facultyDAO.store(related_facultybycommitteemember3);
		facultyDAO.flush();

		facultyDAO.remove(related_facultybycommitteemember3);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeFacultyByCommitteeMember5(Integer thesiscommittee_thesisCommitteeId, Integer related_facultybycommitteemember5_facultyId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);
		Faculty related_facultybycommitteemember5 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember5_facultyId, -1, -1);

		thesiscommittee.setFacultyByCommitteeMember5(null);
		related_facultybycommitteemember5.getThesiscommitteesForCommitteeMember5().remove(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember5 = facultyDAO.store(related_facultybycommitteemember5);
		facultyDAO.flush();

		facultyDAO.remove(related_facultybycommitteemember5);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeFacultyByCommitteeMember2(Integer thesisCommitteeId, Faculty related_facultybycommitteemember2) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Faculty existingfacultyByCommitteeMember2 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember2.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyByCommitteeMember2 != null) {
			existingfacultyByCommitteeMember2.setFacultyId(related_facultybycommitteemember2.getFacultyId());
			existingfacultyByCommitteeMember2.setNnumber(related_facultybycommitteemember2.getNnumber());
			existingfacultyByCommitteeMember2.setFirstName(related_facultybycommitteemember2.getFirstName());
			existingfacultyByCommitteeMember2.setLastName(related_facultybycommitteemember2.getLastName());
			existingfacultyByCommitteeMember2.setEmail(related_facultybycommitteemember2.getEmail());
			existingfacultyByCommitteeMember2.setUrl(related_facultybycommitteemember2.getUrl());
			existingfacultyByCommitteeMember2.setActive(related_facultybycommitteemember2.getActive());
			related_facultybycommitteemember2 = existingfacultyByCommitteeMember2;
		} else {
			related_facultybycommitteemember2 = facultyDAO.store(related_facultybycommitteemember2);
			facultyDAO.flush();
		}

		thesiscommittee.setFacultyByCommitteeMember2(related_facultybycommitteemember2);
		related_facultybycommitteemember2.getThesiscommitteesForCommitteeMember2().add(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember2 = facultyDAO.store(related_facultybycommitteemember2);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeFacultyByCommitteeMember5(Integer thesisCommitteeId, Faculty related_facultybycommitteemember5) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Faculty existingfacultyByCommitteeMember5 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember5.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyByCommitteeMember5 != null) {
			existingfacultyByCommitteeMember5.setFacultyId(related_facultybycommitteemember5.getFacultyId());
			existingfacultyByCommitteeMember5.setNnumber(related_facultybycommitteemember5.getNnumber());
			existingfacultyByCommitteeMember5.setFirstName(related_facultybycommitteemember5.getFirstName());
			existingfacultyByCommitteeMember5.setLastName(related_facultybycommitteemember5.getLastName());
			existingfacultyByCommitteeMember5.setEmail(related_facultybycommitteemember5.getEmail());
			existingfacultyByCommitteeMember5.setUrl(related_facultybycommitteemember5.getUrl());
			existingfacultyByCommitteeMember5.setActive(related_facultybycommitteemember5.getActive());
			related_facultybycommitteemember5 = existingfacultyByCommitteeMember5;
		} else {
			related_facultybycommitteemember5 = facultyDAO.store(related_facultybycommitteemember5);
			facultyDAO.flush();
		}

		thesiscommittee.setFacultyByCommitteeMember5(related_facultybycommitteemember5);
		related_facultybycommitteemember5.getThesiscommitteesForCommitteeMember5().add(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember5 = facultyDAO.store(related_facultybycommitteemember5);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Save an existing Thesis entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeThesises(Integer thesisCommitteeId, Thesis related_thesises) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Thesis existingthesises = thesisDAO.findThesisByPrimaryKey(related_thesises.getThesisId());

		// copy into the existing record to preserve existing relationships
		if (existingthesises != null) {
			existingthesises.setThesisId(related_thesises.getThesisId());
			existingthesises.setTitle(related_thesises.getTitle());
			existingthesises.setAbstract_(related_thesises.getAbstract_());
			existingthesises.setProposalAttempts(related_thesises.getProposalAttempts());
			existingthesises.setDefenseAttempts(related_thesises.getDefenseAttempts());
			existingthesises.setStartDate(related_thesises.getStartDate());
			existingthesises.setDoi(related_thesises.getDoi());
			related_thesises = existingthesises;
		} else {
			related_thesises = thesisDAO.store(related_thesises);
			thesisDAO.flush();
		}

		related_thesises.getThesiscommittees().add(thesiscommittee);
		thesiscommittee.getThesises().add(related_thesises);
		related_thesises = thesisDAO.store(related_thesises);
		thesisDAO.flush();

		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Return a count of all Thesiscommittee entity
	 * 
	 */
	@Transactional
	public Integer countThesiscommittees() {
		return ((Long) thesiscommitteeDAO.createQuerySingleResult("select count(o) from Thesiscommittee o").getSingleResult()).intValue();
	}

	/**
	 * Delete an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee deleteThesiscommitteeFacultyByCommitteeMember2(Integer thesiscommittee_thesisCommitteeId, Integer related_facultybycommitteemember2_facultyId) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesiscommittee_thesisCommitteeId, -1, -1);
		Faculty related_facultybycommitteemember2 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember2_facultyId, -1, -1);

		thesiscommittee.setFacultyByCommitteeMember2(null);
		related_facultybycommitteemember2.getThesiscommitteesForCommitteeMember2().remove(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember2 = facultyDAO.store(related_facultybycommitteemember2);
		facultyDAO.flush();

		facultyDAO.remove(related_facultybycommitteemember2);
		facultyDAO.flush();

		return thesiscommittee;
	}

	/**
	 * Save an existing Faculty entity
	 * 
	 */
	@Transactional
	public Thesiscommittee saveThesiscommitteeFacultyByCommitteeMember4(Integer thesisCommitteeId, Faculty related_facultybycommitteemember4) {
		Thesiscommittee thesiscommittee = thesiscommitteeDAO.findThesiscommitteeByPrimaryKey(thesisCommitteeId, -1, -1);
		Faculty existingfacultyByCommitteeMember4 = facultyDAO.findFacultyByPrimaryKey(related_facultybycommitteemember4.getFacultyId());

		// copy into the existing record to preserve existing relationships
		if (existingfacultyByCommitteeMember4 != null) {
			existingfacultyByCommitteeMember4.setFacultyId(related_facultybycommitteemember4.getFacultyId());
			existingfacultyByCommitteeMember4.setNnumber(related_facultybycommitteemember4.getNnumber());
			existingfacultyByCommitteeMember4.setFirstName(related_facultybycommitteemember4.getFirstName());
			existingfacultyByCommitteeMember4.setLastName(related_facultybycommitteemember4.getLastName());
			existingfacultyByCommitteeMember4.setEmail(related_facultybycommitteemember4.getEmail());
			existingfacultyByCommitteeMember4.setUrl(related_facultybycommitteemember4.getUrl());
			existingfacultyByCommitteeMember4.setActive(related_facultybycommitteemember4.getActive());
			related_facultybycommitteemember4 = existingfacultyByCommitteeMember4;
		} else {
			related_facultybycommitteemember4 = facultyDAO.store(related_facultybycommitteemember4);
			facultyDAO.flush();
		}

		thesiscommittee.setFacultyByCommitteeMember4(related_facultybycommitteemember4);
		related_facultybycommitteemember4.getThesiscommitteesForCommitteeMember4().add(thesiscommittee);
		thesiscommittee = thesiscommitteeDAO.store(thesiscommittee);
		thesiscommitteeDAO.flush();

		related_facultybycommitteemember4 = facultyDAO.store(related_facultybycommitteemember4);
		facultyDAO.flush();

		return thesiscommittee;
	}
}
